package com.xw.Controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xw.Cache.RedisEventManager;
import com.xw.Contents.RetCodeEnums;
import com.xw.Mail.Receiver.FatchMailThread;
import com.xw.Model.JsonResponse;
import com.xw.Model.api.ClusterInfoModel;
import com.xw.Service.ServerStateService;
import com.xw.Task.GetMailTask;

/**
 * 描述:用于监控服务器状态的接口
 * @author 杨建全
 * @date 2017年6月16日 上午9:32:18
 */
@RestController
@RequestMapping("/state")
public class ServerStateController {
	
	@Autowired
	private ServerStateService serverStateService;
	@Autowired
	private RedisEventManager redisEventManager;
	
	/**
	 * 描述：获取当前服务器集群状态信息
	 * @author 杨建全
	 * @date 2017年6月16日
	 */
	@GetMapping("/clusterInfo")
	public ClusterInfoModel getClusterInfo(){
		return serverStateService.getClusterInfo();
	}
	
	@GetMapping("/restartMailTask")
	public JsonResponse restartMailTask(){
		redisEventManager.sendRebootGetMailTaskEvent();
		return new JsonResponse(RetCodeEnums.SUCCESS);
	}
	
	@GetMapping("/mailTask")
	public JsonResponse mailTask(){
		Map<String,String> stateMap = new HashMap<>();
		for (FatchMailThread fmt : GetMailTask.getMailGetThread()) {
			stateMap.put(fmt.getMailBoxName(), fmt.isAlive()+"");
		}
		return new JsonResponse(RetCodeEnums.SUCCESS,stateMap,"");
	}
}
